home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Storage / Bento / CM / GlbNames.c < prev    next >
Encoding:
Text File  |  1996-08-28  |  33.3 KB  |  751 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        GlbNames.c
  3.  
  4.     Contains:    Container Manager Global Name Values Routines
  5.  
  6.     Written by:    Ira L. Ruben
  7.  
  8.     Owned by:    Ed Lai
  9.  
  10.     Copyright:    © 1991 - 1996 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.          <2>     8/13/96    DM        1362809: disable containers on error
  15.          <4>     3/24/95    EL        1209355:. Forgot to merge in last change.
  16.          <3>     3/24/95    EL        1209355: after writing global name, mark it
  17.                                                     as on disk so it would not be written again
  18.                                                     after flush.
  19.          <2>     8/26/94    EL        #1182319 cmLookupGlobalName now won't
  20.                                                     allocate and deallocate memory.
  21.          <2>     3/31/94    EL        Don't rewrite global name if it is already
  22.                                                     on disk. #1150214
  23.          <1>      2/3/94    EL        first checked in
  24.  
  25.     To Do:
  26. */
  27.  
  28. /*---------------------------------------------------------------------------*
  29.  |                                                                           |
  30.  |                           <<<  GlbNames.c   >>>                           |
  31.  |                                                                           |
  32.  |               Container Manager Global Name Values Routines               |
  33.  |                                                                           |
  34.  |                               Ira L. Ruben                                |
  35.  |                                 12/12/91                                  |
  36.  |                                                                           |
  37.  |                  Copyright Apple Computer, Inc. 1991-1994                 |
  38.  |                           All rights reserved.                            |
  39.  |                                                                           |
  40.  *---------------------------------------------------------------------------*
  41.  
  42.  The routines here handle global names.  Global names are object values that are treated
  43.  speciaially.  Normally object values are written to a container and the value we keep in
  44.  the TOC is the offset to the value (and of course its length).  But we want to keep
  45.  global names around so we can handle such calls as CMGetGlobalName() which return
  46.  the global name value for the object (assuming it is such an object).  We also want to
  47.  efficiently determine if there are attempts at multiple definitions (CMRegister...()) of
  48.  the same global name.
  49.  
  50.  Special flag bits are used in the TOC entry to indicate the value is a global name and
  51.  points to it instead of being an offset.  There is room in the TOC value entry for the
  52.  offset too. It does get filled in when we actually write the global name to the container.
  53.  But the pointer must coexist so we can still get at the name string.  We can get away 
  54.  with this because a TOC value is two longs.  Thus we use one for the offset and one for
  55.  the pointer to the global name.  We therefore have to use the pointer to get at the
  56.  global name to get its length when doing the writing.  But this is getting off the track!
  57.  
  58.  Essentially a global name "belongs" to two data structures. The first is the TOC stucture
  59.  defined in  TOCEnts.h   (or .c).  As just mentioned there is a pointer from the value to
  60.  the global name.  The second data structure is the symbol table which contains the 
  61.  global names themselves.  This is a binary tree.  We can easily walk it when it comes time
  62.  to write the global names out instead of walking the more complicated TOC hunting down
  63.  just the global names (although there is nothing prohibiting that -- it was a time/space
  64.  trade-off).
  65.  
  66.  In addition to the global name in its symbol table entry, there is also a back link to
  67.  the value that points to it.  We can thus go from global name to owning object and vice
  68.  versa.  The back link is used when we write the global names to the container so that we
  69.  can fill in the offset in the object's value as mentioned above.
  70.  
  71.  This is a pretty complicated layout.  There is a potential "trap" in that TOC entries
  72.  can be deleted.  If they are, and it is one that points to a global name, we must delete
  73.  the global name too.  But deletingin in a binary tree is a pain! So what we do is 
  74.  simply mark an entry deleted by nulling out its back pointer.  The trap here is that if
  75.  we're not carful we could end up use a null pointer.  Just thought you would like to 
  76.  know that.
  77. */
  78.  
  79.  
  80. #include <stddef.h>
  81. #include <string.h>
  82. #include <setjmp.h>
  83. #include <stdio.h>
  84.  
  85. #ifndef __CMTYPES__
  86. #include "CMTypes.h"
  87. #endif
  88. #ifndef __CM_API__
  89. #include "CMAPI.h"
  90. #endif
  91. #ifndef __LISTMGR__
  92. #include "ListMgr.h"
  93. #endif
  94. #ifndef __SYMMGR__
  95. #include "SymTbMgr.h"      
  96. #endif
  97. #ifndef __TOCENTRIES__
  98. #include "TOCEnts.h"   
  99. #endif
  100. #ifndef __TOCOBJECTS__
  101. #include "TOCObjs.h"   
  102. #endif
  103. #ifndef __GLOBALNAMES__
  104. #include "GlbNames.h"   
  105. #endif
  106. #ifndef __CONTAINEROPS__
  107. #include "Containr.h"  
  108. #endif
  109. #ifndef __HANDLERS__
  110. #include "Handlers.h"
  111. #endif
  112. #ifndef __FREESPACE__
  113. #include "FreeSpce.h" 
  114. #endif
  115. #ifndef __SESSIONDATA__
  116. #include "Session.h"          
  117. #endif
  118. #ifndef ____ERRORRPT____
  119. #include "ErrorRpt.h"      
  120. #endif
  121. #ifndef __UTILITYROUTINES__
  122. #include "Utility.h"        
  123. #endif
  124.                                                                     
  125.                                                                     CM_CFUNCTIONS
  126.  
  127. /* The following generates a segment directive for Mac only due to 32K Jump Table             */
  128. /* Limitations.  If you don't know what I'm talking about don't worry about it.  The        */
  129. /* default is not to generate the pragma.  Theoritically unknown pragmas in ANSI won't    */
  130. /* choke compilers that don't recognize them.  Besides why would they be looked at if        */
  131. /* it's being conditionally skipped over anyway?  I know, famous last words!                        */
  132.  
  133. #if CM_MPW
  134. #pragma segment GlobalNames
  135. #endif
  136.  
  137.  
  138. /* The following is the "control block" that is used to access a global name symbol         */
  139. /* table.  This is privately known only to this file.  Outside this is referred to via     */
  140. /* an anonymous "void *" pointer.                                                                                                                */
  141.  
  142. struct GlobalNameTbl {                            /* Layout for a Global Name Table "control block":    */
  143.     GlobalNamePtr    globalNameRoot;            /*        ptr to root of global name tree                                */
  144.     ContainerPtr    container;                    /*        ptr to "owning" container control block                */
  145.     CM_SHORT            useCount;                        /*        number of distinct users of this table                */
  146. };                                                                    /* Outside caller's see this as "void *"                        */
  147. typedef struct GlobalNameTbl GlobalNameTbl, *GlobalNameTblPtr;
  148.  
  149. /* The following is used in this file by cmWriteAllGlobalNames() to represent the              */
  150. /* refCon used there.  This acts as a communication path while walking through the            */
  151. /* global names.  See that routine for further details.                                                                    */
  152.  
  153. struct WriteGblCommArea{                    /* cmWriteAllGlobalNames() communication area layout:    */
  154.     ContainerPtr container;                    /*         write only names belonging to this container        */
  155.     CMBoolean      reuseFreeSpace;        /*        true ==> try to reuse free space for names            */
  156. };
  157. typedef struct WriteGblCommArea WriteGblCommArea, *WriteGblCommAreaPtr;
  158.  
  159.  
  160. /*--------------------------------------------------------*
  161.  | cmCreateGlobalNameTbl - create a new global name table |
  162.  *--------------------------------------------------------*
  163.  
  164.  This routine MUST be called before any other global name routine is called. It is used to
  165.  set up the data needed by the other global name routines.  A "reference number" is
  166.  returned to the caller who must pass it to all the other global name routines. Any number
  167.  of global name tables can be created.  By passing the returned "reference number" to the
  168.  other routines they will operate on the corresponding table.
  169.   
  170.  Internally, i.e., in this file, the "reference number" is actually a pointer to a control
  171.  block containing the root pointer to the global name table (set to NULL here) and a use
  172.  count.  The use count is explained in cmUseGlobalNameTbl().
  173.  
  174.  This function returns the pointer if we can allocate the control block. NULL is returned
  175.  if the allocation fails.
  176. */
  177.  
  178. void *cmCreateGlobalNameTbl(ContainerPtr container)
  179. {
  180.     GlobalNameTblPtr t;
  181.     
  182.     t = (GlobalNameTblPtr)CMmalloc(sizeof(GlobalNameTbl));/* a "control block" is born!        */
  183.     if (t == NULL) return (NULL);                                                    /* well I thought it was born!    */
  184.  
  185.     t->globalNameRoot = NULL;
  186.     t->container          = container;                                                /* ...set "owning" container        */
  187.     t->useCount                = 1;                                                                /* 1 user unless cmUse...() done*/
  188.     
  189.     return ((void *)t);                                                                        /* give caller the "refNum"            */
  190. }
  191.  
  192.  
  193. /*-------------------------------------------------------------------------------*
  194.  | cmUseGlobalNameTbl - allow multiple uses of the same global name symbol table |
  195.  *-------------------------------------------------------------------------------*
  196.  
  197.  This allows multiple users of the same global name symbol for the specified container. 
  198.  This can come about due to updating containers which want to use the global name table of
  199.  their target.  They call this routine to register the additional use of the specified 
  200.  table.  It is also returned as the function result.
  201.  
  202.  Here we register the additional use by incrementing a use count of the table.  When
  203.  cmFreeAllGlobalNames() is called to remove the global names it will decrement the use
  204.  count.  Only when the count goes to 0 is the data actually freed.
  205. */
  206.  
  207. void *cmUseGlobalNameTbl(void *table, ContainerPtr container)
  208. {
  209.     ++((GlobalNameTblPtr)table)->useCount;                             /* register the additiona use            */
  210.     ((GlobalNameTblPtr)table)->container = container;     /* set the new owning container        */
  211.     
  212.     return (table);                                                                            /* caller can now use this TOC        */
  213. }
  214.  
  215.  
  216. /*-----------------------------------------------------------------*
  217.  | enterCompare - type name comparison routine for cmEnterSymbol() |
  218.  *-----------------------------------------------------------------*
  219.  
  220.  This routine is "sent" to cmEnterSymbol() to do the proper comparisons for global names.
  221.  The global name tree is based on the names (obviously).  cmEnterSymbol() is a generic
  222.  binary tree routine that requires the comparsion to be supplied by its caller to decide
  223.  on what basis the tree is build.  Hence this routine!
  224.  
  225.  Note, this "static" is intentionally left to default memory model under DOS since it is
  226.  passed as a function pointer to cmEnterSymbol().
  227. */
  228.  
  229. static int enterCompare(const void *g1, const void *g2)
  230. {
  231.     return (strcmp((CM_CHAR *)((GlobalNamePtr)g1)->globalName,
  232.                                  (CM_CHAR *)((GlobalNamePtr)g2)->globalName));
  233. }
  234.  
  235. /*-------------------------------------------------------------------*
  236.  | lookupCompare - type name comparison routine for cmLookupSymbol() |
  237.  *-------------------------------------------------------------------*
  238.  
  239.  This routine is "sent" to cmLookupSymbol() to do the proper comparisons for global names.
  240.  The global name tree is based on the names (obviously).  cmLookupSymbol() is a generic
  241.  binary tree routine that requires the comparsion to be supplied by its caller to decide
  242.  on what basis the tree is build.  Hence this routine!
  243.  
  244.  Note, this "static" is intentionally left to default memory model under DOS since it is
  245.  passed as a function pointer to cmEnterSymbol().
  246. */
  247.  
  248. static int lookupCompare(const void *g1, const CM_UCHAR *name)
  249. {
  250.     return (strcmp((CM_CHAR *)((GlobalNamePtr)g1)->globalName, (CM_CHAR *)name));
  251. }
  252.  
  253.  
  254. /*----------------------------------------------------------*
  255.  | cmCreateGlobalName - create and define a new global name |
  256.  *----------------------------------------------------------*
  257.  
  258.  Creates and defines a new global name symbol table entry in the specified global name
  259.  table.  A global name symbol table entry consists of the name and a back pointer to the
  260.  TOCValue which will point to the symbol table entry to be able to get the value data. The
  261.  back pointer is set to NULL in the newly defined global name entry returned from here.
  262.  
  263.  The function returns a pointer to the created definition.  If dup is true, a pointer to
  264.  a previously defined entry is returned.  If NULL is returned then there was an allocation
  265.  failure and the new type could not be created.
  266.  
  267.  The back pointer is used (when cmWriteAllGlobalNames() is called) when we write global
  268.  names to the container to allow us to fill in the ACTUAL TOC value data with the
  269.  container offset to the value.     We make sure we write the global names to the container
  270.  before we write the TOC.  Thus by the time the TOC is written the offsets will be known.
  271.  
  272.  Note, it is assumed that the back pointer will be set by the caller of 
  273.  cmDefineGlobalName().
  274. */
  275.  
  276. GlobalNamePtr cmCreateGlobalName(const void *table, const CM_UCHAR *globalName,
  277.                                                                  CMBoolean *dup)
  278. {
  279.     GlobalNamePtr g, newGlobalName;
  280.     ContainerPtr    container = ((GlobalNameTblPtr)table)->container;
  281.         
  282.     if ((newGlobalName = (GlobalNamePtr)CMmalloc(sizeof(GlobalName) + strlen((CM_CHAR *)globalName))) != NULL) {
  283.         strcpy((CM_CHAR *)newGlobalName->globalName, (CM_CHAR *)globalName);/* fill in new entry    */
  284.         newGlobalName->theValue = NULL;                                                                /* set elsewhere            */
  285.         g = cmEnterGlobalName(table, newGlobalName, dup);                            /* enter the name            */
  286.     } else {
  287.         g = NULL;
  288.         *dup = false;
  289.     }
  290.     
  291.     return (g);                                                                                                            /* return entry ptr        */
  292. }
  293.  
  294.  
  295. /*---------------------------------------------------------------*
  296.  | cmEnterGlobalName - define an already created new global name |
  297.  *---------------------------------------------------------------*
  298.  
  299.  This routine takes a previously created global name symbol table entry end enters it into
  300.  the global name table.
  301.  
  302.  The function returns the entry pointer as its result when dup is false. It may NOT be the
  303.  input pointer if we are reusing a previously deleted entry.  In that case the input
  304.  pointer is freed.  
  305.  
  306.  If dup is true, a pointer to a previously defined entry is returned.
  307.  
  308.  This routine differs from cmCreateGlobalName() above in that here no global name entry
  309.  is allocated.  Indeed cmCreateGlobalName() does call this routine after it does its
  310.  allocation.  This routine is also called by buildGlobalNameTable() in  TOCEnts.c   to
  311.  enter read global names into the table.
  312.  
  313.  Caution: The caller should have only entered the name into the entry prior to call.  This
  314.                      is because we could return a pointer to a previously freed entry.
  315. */
  316.  
  317. GlobalNamePtr cmEnterGlobalName(const void *table, GlobalNamePtr createdGlobalName,
  318.                                                                 CMBoolean *dup)
  319. {
  320.     GlobalNamePtr g;
  321.     ContainerPtr    container = ((GlobalNameTblPtr)table)->container;
  322.         
  323.     g = (GlobalNamePtr)cmEnterSymbol(createdGlobalName, (void **)&((GlobalNameTblPtr)table)->globalNameRoot, 
  324.                                                                      dup, enterCompare);
  325.     
  326.     if (*dup) {                                                                                                        /* possible dup...            */
  327.         CMfree(createdGlobalName);                                                                    /* we don't need new one*/
  328.         if (g->theValue == NULL)                                                                        /* if "deleted"...            */
  329.             *dup = false;                                                                                            /* ..it's really not dup*/
  330.     } 
  331.     
  332.     return (g);                                                                                                        /* return entry ptr            */
  333. }
  334.  
  335.  
  336. /*------------------------------------------------------------*
  337.  | cmLookupGlobalName - find a previously defined global name |
  338.  *------------------------------------------------------------*
  339.  
  340.  Finds the specified global name defined in the specified global name table.  If found,
  341.  the GlobalNamePtr to the found entry is returned.  If not found NULL is returned.
  342.  
  343.  Note, we allocate a temporary global name table entry here and then free it.  If the
  344.  allocation fails, SessionSuccess, a session status switch, is returned false.  Otherwise
  345.  SessionSuccess is true.
  346. */
  347.  
  348. GlobalNamePtr cmLookupGlobalName(const void *table, const CM_UCHAR *globalName)
  349. {
  350.     GlobalNamePtr g;
  351.     ContainerPtr    container = ((GlobalNameTblPtr)table)->container;
  352.     
  353.     g = (GlobalNamePtr)cmLookupSymbol(globalName, ((GlobalNameTblPtr)table)->globalNameRoot, lookupCompare);
  354.     if (g && g->theValue == NULL) g = NULL;                                            /* chk for deleted name    */
  355.     SessionSuccess = true;
  356.     
  357.     return (g);
  358. }
  359.  
  360.  
  361. /*------------------------------------------------------------------------*
  362.  | cmDefineGlobalNameObject - define an object representing a global name |
  363.  *------------------------------------------------------------------------*
  364.  
  365.  This routine is called to define an TOC object corresponding to a global name in the
  366.  specified container.  It is a special case of the more general cmDefineObject() routine.
  367.  See that routine for a discussion on the parameters.
  368.  
  369.  The function returns a pointer to the object if it was successfully created and NULL if
  370.  it wasn't.  An error is reported if NULL is returned.  Note, while error calls are not
  371.  supposed to return we assume here they due just to be safe!
  372.  
  373.  The only difference between this routine and cmDefineObject() is that the value is passed
  374.  as a global name string instead of a built TOCValueBytes structure.  Finally, we don't
  375.  bother returning a pointer to the value's header because here we should only have one
  376.  value for the global name.  We can always get at it if we need to through the object
  377.  itself.
  378.   
  379.  Here we take the string and build a TOCValueBytes value to point to a global name symbol
  380.  table entry which is also created from here.  The symbol table entry will have a back
  381.  pointer to the value entry we create for the object.
  382.  
  383.  The flags and objectFlags are passed just as in cmDefineObject().  However, the flags are
  384.  ORed with the kCMGlobalName flag.  This marks the value as unique and as a value with a
  385.  pointer to a global name symbol table entry. The ValueGlobal flag echos this in the value
  386.  valueHdr.
  387.  
  388.  The main reasons we don't use cmDefineObject() directly is because of the extra work
  389.  needed to be done for global names to set the back pointer in their entries.  We can also
  390.  do the extra checks for attempts at multiply defining a global name object.
  391. */
  392.  
  393. TOCObjectPtr cmDefineGlobalNameObject(const ContainerPtr container, CM_ULONG objectID,
  394.                                                                            CM_ULONG propertyID, CM_ULONG typeID, 
  395.                                                                              CM_UCHAR *globalName, CM_ULONG generation,
  396.                                                                             CM_USHORT flags, CM_USHORT objectFlags)
  397.  
  398. {
  399.     TOCValueBytes     valueBytes, *v;
  400.     TOCValueHdrPtr theValueHdr;
  401.     TOCValuePtr         theValue;
  402.     TOCObjectPtr     theObject;
  403.     char                     idStr[15];
  404.     
  405.     /* Fill in our value bytes with the pointer to the global name symbol table entry...    */
  406.     
  407.     v = cmSetValueBytes(container, &valueBytes, Value_GlobalName, (CM_ULONG)globalName, 0);
  408.     if (v == NULL) return (NULL);
  409.     
  410.     /* Create the object (hopefully).  The returned theValueHdr will be the header to the    */
  411.     /* object's value chain.  There must be only one value or we have an error.    If we're    */
  412.     /* happy we can set the back pointer in the global name entry.                                                */
  413.     
  414.     theObject = cmDefineObject(container, objectID, propertyID, typeID, &valueBytes,
  415.                                                           generation, (CM_USHORT)(flags | kCMGlobalName),
  416.                                                          objectFlags, &theValueHdr);
  417.                                                      
  418.     if (theObject != NULL)
  419.         if (cmCountListCells(&theObject->propertyList) > 1 ||
  420.                 cmCountListCells(&((TOCPropertyPtr)cmGetListHead(&theObject->propertyList))->valueHdrList) > 1) {
  421.             Container_Disable(container);
  422.             ERROR3(CM_err_MultGlblNames, cmltostr(theObject->objectID, 1, false, idStr),
  423.                                                                      globalName, CONTAINERNAME);
  424.             theObject = NULL;
  425.         } else {
  426.             theValue = (TOCValuePtr)cmGetListHead(&theValueHdr->valueList);    /* tail if u want    */
  427.             SetGlobalNameValueBackPtr(theValue->value.globalName.globalNameSymbol, theValue);
  428.             if ((objectFlags & ProtectedObject) != 0)                /* if object is protected....            */
  429.                 theValueHdr->valueFlags |= ValueProtected;        /* ...then protect its value too    */
  430.         }
  431.     
  432.     return (theObject);                                            /* return ptr to the created object                        */
  433. }
  434.  
  435.  
  436. /*----------------------------------------------------------*
  437.  | cmForEachGlobalName - do some action on each global name |
  438.  *----------------------------------------------------------*
  439.  
  440.  Do (call) the specified action for each defined global name in the specified global name
  441.  symbol table. The pointer to each global name entry is passed to the action routine along
  442.  with a "refCon" which the caller can use as a communication facility to convey additional
  443.  info to the action routine. 0 is returned to indicate successfull completion.  Use the
  444.  AbortForEachGlobalName(x) (a macro) in the action routine to abort the interator.  The 
  445.  "x" is a integer which is returned from cmForEachGlobalName() so it should not be 0. 
  446. */
  447.  
  448. int cmForEachGlobalName(const void *table, CMRefCon refCon,
  449.                                                 void (*action)(GlobalNamePtr globalName, CMRefCon refCon))
  450. {
  451.     ContainerPtr container = ((GlobalNameTblPtr)table)->container;
  452.     
  453.     if (setjmp(SESSION->cmForEachGlobalNameEnv))            /* if longjmp taken...                            */
  454.         return (1);                                                                            /* ...report the bad news                        */
  455.         
  456.     cmForEachSymbol(((GlobalNameTblPtr)table)->globalNameRoot, refCon, (SymbolAction)action);
  457.     
  458.     return (0);
  459. }
  460.  
  461.  
  462. /*---------------------------------------------------------*
  463.  | write1GlobalName - write a global name to its container |
  464.  *---------------------------------------------------------*
  465.  
  466.  This routine is the action routine for cmWriteAllGlobalNames() to write one global name
  467.  string to its container.  We only write the name if the container field in the value
  468.  segment entry for the global name equals the container pointer in the refCon.  This was
  469.  passed as the updatingContainer to cmWriteAllGlobalNames().
  470.  
  471.  Global name strings are NOT written when they are created (by  cmDefineGlobalName()). They
  472.  are kept in as global name symbol table entries with an object's value data (TOCValue)
  473.  pointing to a symbol table entry for global data objects.  When the object is created
  474.  the global name entry is set with a back pointer to the object's TOCValue value.
  475.  
  476.  Using the back pointer we can get at all the structures of the object and its container.
  477.  Thus we write out the global name string to its container and SET the data value in the
  478.  objects TOCValue value to the offset to where we wrote the string in the container.
  479.  
  480.  The global names are written to the container before we write the TOC.  Thus by the time
  481.  the TOC is written it will have all the offset correct for the global names.  The TOC
  482.  value writer will still have to access the global name entry to get the length which is
  483.  also part of the TOC.  Thus the TOCValue value data for global name will, after we
  484.  complete the global name writing contains two pieces of information: the container offset
  485.  and its global name symbol symbol table pointer.
  486.  
  487.  Note, if a value for an object has been deleted, then the back pointer is set to NULL to
  488.  indicate that fact.  It's a pain to delete entries from a binary tree, so marking an
  489.  entry as deleted is simpler.  We will see the deleted entries as we walk the symbol table
  490.  tree.  The check for the deleted entries must therefore be made.
  491.  
  492.  The only flaw in this is that a newly created, but not completed entry from a call to
  493.  cmDefineGlobalName() also inits the back pointer to NULL. If used correctly (ha!) caller's
  494.  to cmDefineGlobalName() are expected to fill in the back pointer.  Things should go as
  495.  expected.  But if they don't...
  496.  
  497.  Note, this "static" is intentionally left to default memory model under DOS since it is
  498.  passed as a function pointer to cmForEachGlobalName().
  499. */
  500.  
  501. static void write1GlobalName(GlobalNamePtr globalNameSymbol, CMRefCon refCon)
  502. {
  503.     WriteGblCommAreaPtr g = (WriteGblCommAreaPtr)refCon;
  504.     TOCValuePtr                     theValue = globalNameSymbol->theValue;
  505.     TOCValueHdrPtr             theValueHdr;
  506.     TOCObjectPtr                 theObject;
  507.     ContainerPtr                  container;
  508.     CM_ULONG                         nameLength, offset, actualSize;
  509.     
  510.     if (theValue == NULL) return;                                            /* exit if name was deleted                    */
  511.  
  512.     if (theValue->container != g->container) return;    /* exit if name is not desired            */
  513.     
  514.     theValueHdr = theValue->theValueHdr;
  515.     container   = theValueHdr->container->updatingContainer;
  516.  
  517.     if ((theValueHdr->valueFlags & ValueGlobal) == 0) {
  518.         Container_Disable(container);
  519.         ERROR1(CM_err_NotGlobalName, CONTAINERNAME);
  520.         AbortForEachGlobalName(CM_err_NotGlobalName);
  521.     }
  522.     
  523.     if (theValueHdr->valueRefCon) {                            /* has a TOC means already on disk    */
  524.         return;
  525.     }
  526.     /* Don't write standard object names...                                                                                                */
  527.     
  528.     theObject = theValueHdr->theProperty->theObject;
  529.     if (theObject->objectID > CM_StdObjID_TOC && theObject->objectID < MinUserObjectID)
  530.         return;
  531.         
  532.     /* If we are reusing free space for updating, try to get offset from free list.  If        */
  533.     /* we are not updating, the seek to the end of the container was alread done.                    */
  534.     
  535.     nameLength  = strlen((CM_CHAR *)globalNameSymbol->globalName) + 1;
  536.     
  537.     if (g->reuseFreeSpace) {                                                    /* if we are to reuse free space...    */
  538.         offset = cmGetFreeListEntry(container, nameLength, true, &actualSize); /* do it            */
  539.         if (actualSize != 0)                                                        /* if we got some to reuse...                */
  540.             CMfseek(container, offset, kCMSeekSet);                /* ...position to write over it            */
  541.         else {                                                                                     /* if couldn't find a fit...                */
  542.             CMfseek(container, 0, kCMSeekEnd);                        /* ...write data to end of container*/
  543.             offset = container->physicalEOF;                            /* used to update highest written        */
  544.         }
  545.     } else {
  546.         actualSize = 0;                                                                    /* use as switch to update eof info    */
  547.         offset = container->physicalEOF;                                /* used to update highest written        */
  548.     }
  549.     
  550.     /* Write the global name as a single segment...                                                                                */
  551.     
  552.     if (CMfwrite(container, globalNameSymbol->globalName, sizeof(CM_UCHAR), nameLength) != nameLength) {
  553.         Container_Disable(container);
  554.         ERROR2(CM_err_BadGNameWrite, globalNameSymbol->globalName, CONTAINERNAME);
  555.         AbortForEachGlobalName(CM_err_BadGNameWrite);
  556.     }
  557.         
  558.     /* define offset for global name, extra offset if merging                                                            */
  559.     theValue->value.globalName.offset = offset + container->mergeInSize;
  560.     
  561.     offset += nameLength;                                                            /* update logical OR physical EOF        */
  562.     if (actualSize == 0)
  563.         container->physicalEOF = offset;                                /* update EOF for next global name    */
  564.     SetLogicalEOF(offset);                                                        /* set logical EOF (may != physical)*/
  565.  
  566.     theValueHdr->valueRefCon = container->toc;                /* remember it is now on disk                */    
  567. }
  568.  
  569.  
  570. /*-------------------------------------------------------------------*
  571.  | cmWriteAllGlobalNames - write all global names to their container |
  572.  *-------------------------------------------------------------------*
  573.  
  574.  This routine is called by CMCloseContainer() just prior to writing the TOC to the
  575.  container to make sure all the global names in the specified global name symbol table are
  576.  written to the (end of the) container first.  Global names are thus the last data objects
  577.  in a container before the TOC.
  578.  
  579.  The function returns true if the global names were successfully written and false
  580.  otherwise.  As usual this is as safety since the error handler should not normmaly
  581.  return.
  582.  
  583.  Note, only global names in the specified symbol table that are "owned" by the container
  584.  are written.  A container used to update a target container utilize a global symbol table
  585.  common to both.  However, only NEW global names  that are to be recorded as belonging to
  586.  the updating container are to be written into that container.
  587.  
  588.  We keep global names in memory to make it easier to get at them for such API routines as
  589.  CMGetGlobalName().  Another use is to efficiently determine if there are attempts at
  590.  multiple definitions (CMRegister...()) of the same global name.
  591.  
  592.  The price we pay for this is a complication of TOC entries in that the corresponding 
  593.  value for a global name is NOT a container offset prior to writing.  It is a pointer to
  594.  its global name symbol table entry!  By the time we return from here an offset WILL be
  595.  in the value along with the symbol table pointer.  Funny thing about that!  Just in time
  596.  for the TOC write.
  597. */
  598.  
  599. CMBoolean cmWriteAllGlobalNames(const void *table)
  600. {
  601.     WriteGblCommArea gblCommArea;
  602.     ContainerPtr         container = ((GlobalNameTblPtr)table)->container;
  603.     
  604.     gblCommArea.container = container;                                            /* write only names from this    */
  605.     
  606.     if ((container->useFlags & kCMReuseFreeSpace) == 0) {        /* if not reusing free space..*/
  607.         CMfseek(container, 0, kCMSeekEnd);                                        /* ..write to end of container*/
  608.         gblCommArea.reuseFreeSpace = false;                                        /* ...suppress seeks                    */
  609.     } else                                                                                                    /* if updating, resue space...*/
  610.         gblCommArea.reuseFreeSpace = (CMBoolean)((container->useFlags & kCMReuseFreeSpace) != 0);
  611.  
  612.     container->physicalEOF = CMgetContainerSize(container);
  613.     return ((CMBoolean)(cmForEachGlobalName(table, (CMRefCon)&gblCommArea, write1GlobalName) == 0));
  614. }
  615.  
  616.  
  617. /*----------------------------------------------*
  618.  | cmFreeAllGlobalNames - free all global names |
  619.  *----------------------------------------------*
  620.  
  621.  This routine is called to remove the definitions of ALL previously defined global names
  622.  in the specified global name symbol table for a container.  The caller's symbol table
  623.  pointer is set to NULL prior to return.
  624.  
  625.  Note, there may be multiple users of a global name symbol table. The use count is
  626.  decremented and the symbol table only freed if the count goes to 0.
  627. */
  628.  
  629. void cmFreeAllGlobalNames(void **table)
  630. {
  631.     ContainerPtr container = ((GlobalNameTblPtr)*table)->container;
  632.  
  633.     if (--((GlobalNameTblPtr)*table)->useCount > 0)             /* don't free if multiple users    */
  634.         return; 
  635.  
  636.     cmFreeAllSymbols((void **)&((GlobalNameTblPtr)*table)->globalNameRoot, SESSION);
  637.  
  638.     CMfree(*table);                                                                                /* bye, bye table                                */
  639.     *(GlobalNameTblPtr **)table = NULL;                                        /* kill caller's table pointer    */
  640. }
  641.  
  642.  
  643. /*------------------------------------------------------------------------------------*
  644.  | cmIsGlobalNameObject - determine if a type or property object is for a global name |
  645.  *------------------------------------------------------------------------------------*
  646.  
  647.  Given a pointer to a type or property object, which is expected to have the corresponding
  648.  standard global name object ID defined by the macros CM_StdObjID_GlobalTypeName and 
  649.  CM_StdObjID_GlobalPropName, return the pointer to the global name string if the object is
  650.  indeed for a global name.  NULL is returned if the object isn't for a global type or
  651.  property name.
  652.  
  653.  This routine only returns the pointer to the global name string because that is currently
  654.  all we want to know from the object.  If it later turns out we need the actual global
  655.  name entry then all that needs to be changed is the return pointer.  Of course then 
  656.  callers after only the name string will have to do the access.
  657. */
  658.  
  659. CM_CHAR *cmIsGlobalNameObject(TOCObjectPtr typePropertyObject, CM_ULONG objectID)
  660. {
  661.     TOCPropertyPtr theProperty;
  662.     TOCValueHdrPtr theValueHdr;
  663.     TOCValuePtr         theValue;
  664.     
  665.     /* Check everything that can possibly be checked to make sure we indeed do have a         */
  666.     /* global name.  Here's what we must have to convenience ourselves of that fact:            */
  667.  
  668.     /*        (1). The property with the specified objectID must be part of the object.                */
  669.     /*        (2). There must be a value header defined for the property.                                            */
  670.     /*        (3). The type ID in that value header must be CM_StdObjID_7BitASCII.                        */
  671.     /*        (4). There must be a value for the value header.                                                                */
  672.     /*        (5). The flags in that value must contain kCMGlobalName.                                                */
  673.  
  674.     /* If we get through all that, we're happy!                                                                                        */
  675.     
  676.     if ((theProperty = cmGetObjectProperty(typePropertyObject, objectID)) != NULL)
  677.         if ((theValueHdr = (TOCValueHdrPtr)cmGetListHead(&theProperty->valueHdrList)) != NULL)
  678.             if (theValueHdr->typeID == CM_StdObjID_7BitASCII)
  679.                 if ((theValue = (TOCValuePtr)cmGetListHead(&theValueHdr->valueList)) != NULL)
  680.                     if ((theValue->flags & kCMGlobalName) == 0)
  681.                         theValue = NULL;
  682.     
  683.     if (theProperty == NULL || theValueHdr == NULL || theValue == NULL)
  684.         return (NULL);
  685.         
  686.     return (GetGlobalName(theValue->value.globalName.globalNameSymbol));
  687. }
  688.  
  689.  
  690. /*-------------------------------------------------------------*
  691.  | cmGetGlobalTypeName - return global name for type object ID |
  692.  *-------------------------------------------------------------*
  693.  
  694.  This routine takes an object ID assumed to be a type object, and returns the global name
  695.  for that type.  A string is always returned.  If the ID does not correspond to a type,
  696.  "?" is returned as the string.
  697.  
  698.  Note, this routine differs from cmIsGlobalNameObject() in that here an object ID is
  699.  converted to its corresponding global name.  In cmIsGlobalNameObject(), it is assumed the
  700.  object has already been found.  So all we have to do here is do the find and then call
  701.  cmIsGlobalNameObject() to get the name.
  702. */
  703.  
  704. CM_CHAR *cmGetGlobalTypeName(ContainerPtr container, CM_ULONG typeID)
  705. {
  706.     TOCObjectPtr     type;
  707.     CM_CHAR                *typeName;
  708.  
  709.     type = cmFindObject(container->toc, (CMObjectID)typeID);
  710.     
  711.     typeName = (type == NULL) ? NULL : cmIsGlobalNameObject(type, CM_StdObjID_GlobalTypeName);
  712.     
  713.     if (typeName == NULL || *typeName == '\0') typeName = "?";
  714.     
  715.     return (typeName);
  716. }
  717.  
  718.  
  719. /*---------------------------------------------------------------------*
  720.  | cmGetGlobalPropertyName - return global name for property object ID |
  721.  *---------------------------------------------------------------------*
  722.  
  723.  This routine takes an object ID assumed to be a property object, and returns the global
  724.  name for that property.  A string is always returned.  If the ID does not correspond to a
  725.  type, "?" is returned as the string.
  726.  
  727.  Note, this routine differs from cmIsGlobalNameObject() in that here an object ID is
  728.  converted to its corresponding global name.  In cmIsGlobalNameObject(), it is assumed the
  729.  object has already been found.  So all we have to do here is do the find and then call
  730.  cmIsGlobalNameObject() to get the name.  This is exactly like cmGetGlobalTypeName() but
  731.  for property names instead of types.
  732.  
  733.  Both these routines are mainly used to generate error message inserts.
  734. */
  735.  
  736. CM_CHAR *cmGetGlobalPropertyName(ContainerPtr container, CM_ULONG propertyID)
  737. {
  738.     TOCObjectPtr     property;
  739.     CM_CHAR                *propertyName;
  740.  
  741.     property = cmFindObject(container->toc, (CMObjectID)propertyID);
  742.     
  743.     propertyName = (property == NULL) ? NULL : cmIsGlobalNameObject(property, CM_StdObjID_GlobalPropName);
  744.     
  745.     if (propertyName == NULL || *propertyName == '\0') propertyName = "?";
  746.     
  747.     return (propertyName);
  748. }
  749.                                                           
  750.                                                             CM_END_CFUNCTIONS
  751.